package com.example.onlinetaxi.ui.activity.StartToEndPage;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.baidu.mapapi.map.MapView;
import com.example.onlinetaxi.BaiDuMap.BaiDuMapManager;
import com.example.onlinetaxi.BaiDuMap.RoutePlan.RoutePlan;
import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.LocalStroage.PassengerRouteLine;
import com.example.onlinetaxi.R;
import com.example.onlinetaxi.ui.activity.BaseActivity;

import java.util.HashMap;
import java.util.Map;

public class StartToEndActivity extends BaseActivity {
    // 位置信息
    public static Map<String, String> startMap = new HashMap<>();
    public static Map<String, String> endMap = new HashMap<>();
    // 乘客前往终点
    private PassengerArrivedFragment passengerArrivedFragment = new PassengerArrivedFragment();
    // 司机在前往终点
    private DriverArrivedFragment driverArrivedFragment = new DriverArrivedFragment();
    // 地图
    private MapView mMapView;
    // 返回控件
    private ImageView backToMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_to_end);

        // 先将上一个百度地图信息销毁
        BaiDuMapManager.baiDuMapManager.destroy();

        init();
    }

    private void init() {
        // 配置订单信息
        startMap.put("city", LocalStorage.currentOrder.getStartCity());
        startMap.put("place", LocalStorage.currentOrder.getStartInfo());
        endMap.put("city", LocalStorage.currentOrder.getEndCity());
        endMap.put("place", LocalStorage.currentOrder.getEndInfo());

        if (PassengerRouteLine.drivingRouteLine == null || PassengerRouteLine.drivingRouteLine.getAllStep() == null) {
            // 找到路线
            RoutePlan.findRoute();
        }

        // 初始化布局
        initView();

        initEvent();
    }

    private void initEvent() {
        backToMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 关闭当前页面
                finish();
            }
        });
    }

    private void initView() {
        // 初始化地图
        mMapView = findViewById(R.id.driver_to_end_map);
        backToMain = findViewById(R.id.start_to_end_to_main);
        // 初始化地图管理者
        BaiDuMapManager.baiDuMapManager.setActivity(this);  // 先传递依附主体
        BaiDuMapManager.baiDuMapManager.isLocate(true);
        BaiDuMapManager.baiDuMapManager.isOpenGeoReverse(false);
        BaiDuMapManager.baiDuMapManager.setMapView(mMapView);

        Toast.makeText(this, "司机已接单,正在路上!", Toast.LENGTH_SHORT).show();

        // 设置当前Fragment
        if (LocalStorage.type.equals("driver")) {
            replaceFragment(driverArrivedFragment);
        } else {
            replaceFragment(passengerArrivedFragment);
        }

        // 绘制小车
        drawRoute();
    }

    // 绘制小车移动（司机到起点）
    public void drawRoute() {
        // 创建路径规划对象
        RoutePlan routePlan = new RoutePlan(mMapView);

        // 进行绘制(路线和小车移动)
        routePlan.drawRouteAndCar2();
    }

    // 进行碎片之间的切换
    private void replaceFragment(Fragment fragment) {
        // 获取碎片管理类
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启碎片事物
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 创建事务
        fragmentTransaction.replace(R.id.driver_info_fragment_to_end, fragment);
        // 提交事务
        fragmentTransaction.commit();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 在activity执行onResume时必须调用mMapView.onResume()
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时必须调用mMapView.onPause()
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BaiDuMapManager.baiDuMapManager.destroy();
//        routePlan.clear();
    }
}